﻿package plugin.npcDialog.ui.panels.td
{
    import com.core.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.instance.*;
    import pluginfw.nio.*;
    import uiresource.npcdialogs.*;
    import view.light.*;
    import view.message.*;
    import view.uilt.*;

    public class TdPanel extends TdUI implements INpcDialogContent
    {
        private var mapid:int = 0;
        private var _init:Boolean = false;
        private var instance:Object;
        private var _isfirst:Boolean = true;

        public function TdPanel()
        {
            return;
        }// end function

        public function close() : void
        {
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            return;
        }// end function

        public function show(param1:Object, param2:Object) : void
        {
            var _loc_3:* = ConfigDictionary.data.intoinstance;
            var _loc_4:Array = [];
            var _loc_5:* = _loc_3[param1.npcid];
            this.mapid = _loc_5.mapid;
            this.instance = ConfigDictionary.data.instance[this.mapid];
            label_lv.text = this.instance.open_level;
            var _loc_6:* = this.instance.limit_times;
            var _loc_7:* = this.instance.limit_times.split(" ");
            var _loc_8:int = 0;
            var _loc_9:* = ModelProxy.getValue("data", "dayinstances");
            if (ModelProxy.getValue("data", "dayinstances") == null || !_loc_9.hasOwnProperty(this.mapid))
            {
                _loc_8 = _loc_7[1];
            }
            else
            {
                _loc_8 = _loc_7[1] - int(_loc_9[this.mapid]);
            }
            label_num.text = _loc_8 + "";
            var _loc_10:* = this.instance.total_time / 60;
            label_time.text = "副本时间限制" + _loc_10 + "分钟";
            label_jieshao.text = this.instance.tips;
            var _loc_11:* = UserData.getOnlineData("supertd");
            if (UserData.getOnlineData("supertd") && _loc_11[0] > 0)
            {
                startbox.visible = false;
                endbox.visible = true;
                this.updatereward();
            }
            else
            {
                startbox.visible = true;
                endbox.visible = false;
            }
            return;
        }// end function

        override protected function initialize() : void
        {
            super.initialize();
            this.listener();
            return;
        }// end function

        private function listener() : void
        {
            btn_js.addEventListener(MouseEvent.CLICK, this.btn_jsClickHandler);
            return;
        }// end function

        protected function btn_jsClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = ConfigDictionary.data.instance[this.mapid];
            if (ModelProxy.getValue("data", "user.level") < _loc_2.open_level)
            {
                WarningManager.show(38, [_loc_2.open_level]);
                return;
            }
            PluginManager.instance.sendToServer("instance", CMDInstance.NpcInstance, {mapid:this.mapid, type:0});
            ModelProxy.change("plugin", "close", "npcdialog");
            return;
        }// end function

        public function openRefresh(param1:Object) : void
        {
            var _loc_2:* = ConfigDictionary.data.intoinstance;
            var _loc_3:Array = [];
            var _loc_4:* = _loc_2[param1.npcid];
            var _loc_5:* = _loc_2[param1.npcid].mapid;
            this.instance = ConfigDictionary.data.instance[_loc_5];
            label_lv.text = this.instance.open_level;
            var _loc_6:* = this.instance.limit_times;
            var _loc_7:* = this.instance.limit_times.split("#");
            var _loc_8:int = 0;
            var _loc_9:* = ModelProxy.getValue("data", "dayinstances");
            if (ModelProxy.getValue("data", "dayinstances") == null || !_loc_9.hasOwnProperty(_loc_5))
            {
                _loc_8 = _loc_7[1];
            }
            else
            {
                _loc_8 = _loc_7[1] - _loc_9.mapid;
            }
            label_num.text = _loc_8 + "";
            var _loc_10:* = this.instance.total_time / 60;
            label_time.text = "副本时间限制" + _loc_10 + "分钟";
            label_jieshao.text = this.instance.tips;
            var _loc_11:* = UserData.getOnlineData("supertd");
            if (UserData.getOnlineData("supertd") && _loc_11[0] > 0)
            {
                startbox.visible = false;
                endbox.visible = true;
                this.updatereward();
            }
            else
            {
                startbox.visible = true;
                endbox.visible = false;
            }
            return;
        }// end function

        private function updatereward() : void
        {
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_1:* = UserData.getOnlineData("supertd");
            label_hdexp.text = _loc_1[0] * 1000 + "";
            label_diushiexp.text = _loc_1[1] * 1000 + "";
            var _loc_2:Array = [];
            var _loc_3:* = ConfigDictionary.data.tdreward;
            this._isfirst = true;
            for each (_loc_4 in _loc_3)
            {
                
                _loc_5 = new Object();
                _loc_5.data = _loc_4;
                _loc_5.exp = _loc_4.rate * _loc_1[0] * 1000;
                if (!ItemUilt.checkVIPLevel(_loc_5.data.vip))
                {
                    if (this._isfirst)
                    {
                        _loc_2.push(_loc_5);
                        this._isfirst = false;
                    }
                    continue;
                }
                _loc_2.push(_loc_5);
            }
            list.array = _loc_2;
            list.selectedIndex = -1;
            list.renderHandler = new Handler(this.msgRender);
            return;
        }// end function

        private function msgRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            if (param2 < list.length)
            {
                _loc_3 = ModelProxy.getValue("vip", "getvip");
                _loc_4 = _loc_3.vipLevel;
                LightFactory.clearlight(param1.getChildByName("lingqu") as LinkButton);
                (param1.getChildByName("lingqu") as LinkButton).labelColors = "0x54B436,0xefd742,0x54b436,0xefd742";
                if (param2 == (list.length - 1))
                {
                    LightFactory.showlight(param1.getChildByName("lingqu") as LinkButton);
                }
                (param1.getChildByName("exp") as Label).text = param1.dataSource.exp;
                (param1.getChildByName("label_lingqu") as Label).text = param1.dataSource.data.rate + "倍领取：";
                param1.getChildByName("lingqu").removeEventListener(MouseEvent.CLICK, this.clickhandler);
                (param1.getChildByName("lingqu") as LinkButton).removeEventListener(MouseEvent.ROLL_OVER, this.rollOverHandler);
                (param1.getChildByName("lingqu") as LinkButton).removeEventListener(MouseEvent.ROLL_OUT, this.rollOutHandler);
                param1.getChildByName("lingqu").addEventListener(MouseEvent.CLICK, this.clickhandler);
                (param1.getChildByName("lingqu") as LinkButton).addEventListener(MouseEvent.ROLL_OVER, this.rollOverHandler);
                (param1.getChildByName("lingqu") as LinkButton).addEventListener(MouseEvent.ROLL_OUT, this.rollOutHandler);
            }
            return;
        }// end function

        protected function rollOutHandler(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        protected function rollOverHandler(event:Event) : void
        {
            var _loc_2:* = (event.currentTarget as LinkButton).parent as Component;
            var _loc_3:* = _loc_2.dataSource.data.rate + "倍领取";
            if (_loc_2.dataSource.data.vip > 0)
            {
                _loc_3 = _loc_3 + ("(VIP" + _loc_2.dataSource.data.vip + ")");
            }
            ModelProxy.change("tips", "normal", _loc_3);
            return;
        }// end function

        protected function clickhandler(event:MouseEvent) : void
        {
            var output:Output;
            var event:* = event;
            var item:* = (event.currentTarget as LinkButton).parent as Component;
            var result:* = ModelProxy.getValue("vip", "getvip");
            var viplevel:* = result.vipLevel;
            if (!ItemUilt.checkVIPLevel(item.dataSource.data.vip))
            {
                WarningManager.show(116, [item.dataSource.data.vip]);
                return;
            }
            if (item.dataSource.data.itemid == 3 && ItemUilt.getAllCount(item.dataSource.data.itemid) < item.dataSource.data.cost)
            {
                WarningManager.show(42);
                return;
            }
            if (item.dataSource.data.itemid == 3)
            {
                Alert2.show("是否花费 " + MColor.getHMTL(item.dataSource.data.cost, 6) + "元宝领取" + MColor.getHMTL(item.dataSource.exp, 6) + "经验", "", ["确认", "取消"], function (param1:int, param2:Object) : void
            {
                var _loc_3:Output = null;
                if (param1 == 0)
                {
                    _loc_3 = new Output();
                    _loc_3.writeVarInt(param2.rate, false);
                    PluginManager.instance.sendToBufferServer("instance", CMDInstance.GetTDReward, _loc_3);
                    ModelProxy.change("plugin", "close", "npcdialog");
                }
                return;
            }// end function
            , item.dataSource.data);
            }
            else
            {
                output = new Output();
                output.writeVarInt(item.dataSource.data.rate, false);
                PluginManager.instance.sendToBufferServer("instance", CMDInstance.GetTDReward, output);
                ModelProxy.change("plugin", "close", "npcdialog");
            }
            return;
        }// end function

        public function update(param1:Object, param2:int = 0) : void
        {
            return;
        }// end function

    }
}
